import re
import sqlite3
from tqdm import tqdm
import ast
from my_db import SQLiteCRUD

db = SQLiteCRUD('logdb.db')
try:
    db.connect()
    db.create_table("logsdb", {
                "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
                "timestamp": "TEXT",
                "level": "TEXT",
                "module": "TEXT",
                "function": "TEXT",
                "line": "TEXT",
                "log_type": "TEXT",
                "details": "TEXT",
                "userTagAnalysis": "TEXT",
                "topElements": "TEXT",
                "questions": "TEXT",
                "message": "TEXT",
                "messageId": "TEXT",
                'dialogId': "TEXT",
                'history': "TEXT"
            })
except Exception as e:
    print(e)
# 定义日志文件路径

# log_file_path = 'logs/mylog-info.2025-01-23_05-35-30_457697.log'
# log_file_path = 'logs/mylog-info.2025-02-12_06-03-48_397436.log'
log_file_path = 'logs/mylog-info.2025-02-14_04-27-47_400529.log'
# log_file_path = 'logs/mylog-info.log'
# log_file_path = 'logs/db-info.2024-12-30_16-56-37_744863.log'
# 读取日志文件
with open(log_file_path, 'r', encoding='utf-8') as log_file:
    logs = log_file.readlines()
count = 0
# 处理每一行日志并插入到数据库
for log in tqdm(logs):
    log_line = log.strip().split('|')
    if log_line.__len__() < 4:
        continue
    timestamp = log_line[0].strip()
    level = log_line[1].strip()
    module = log_line[2].strip().split(':')[0]
    function = log_line[2].strip().split(':')[1]
    line = log_line[2].strip().split(':')[2]
    message = ""
    messageId = ""
    dialogId = ""
    history = ""
    userTagAnalysis = ""
    topElements = ""
    questions = ""
    log_type = ""
    if log_line.__len__()>3:
        log_line[3] = ''.join(log_line[3:])
    if "rsp_data" in log_line[3]:
        log_type = "Response"
        details = log_line[3].strip().split('rsp_data:')[1]
        if "message" in eval(details):
            message = eval(details)["message"]
            messageId = eval(details)["messageId"]
        count +=1
    elif "requst_data" in log_line[3]:
        log_type = "Request"
        details = log_line[3].strip().split('requst_data:')[1]
        if "dialogId" in eval(details):
            dialogId = eval(details)["dialogId"]
        if "history" in eval(details):
            history = eval(details)["history"]
            message = eval(details)["message"]
            userTagAnalysis = eval(details)["userTagAnalysis"]
            topElements = eval(details)["topElements"]
            questions = eval(details)["questions"]
            # question = eval(details)["question"]
        count +=1
    elif "==========" in log_line[3]:
        pass
    else:
        print("未匹配到日志类型")
    user_id = db.insert("logsdb", {
        "timestamp": timestamp,
        "level": level,
        "module": module,
        "function": function,
        "line": line,
        "log_type": log_type,
        "details": details,
        "userTagAnalysis": userTagAnalysis,
        "topElements": topElements,
        "questions": questions,
        "message": message,
        "messageId": messageId,
        "dialogId": dialogId,
        "history": str(history)
    })
db.close()
print("日志信息已成功写入SQLite数据库。写入条数%d"%count)